/*
 * jQuery Select Plugins v1.3.6.1
 * Copyright (c) 2009 zhangjingwei
 * Dual licensed under the MIT and GPL licenses.
 * Date: 2009-11-17 09:37
 * Revision: 1.3.6.1
 * 
 * Demo html code:
 * <div id="myselect">
 *     <select id="mybirthday" name="mybirthday">
 *             <option value="">select birthday</option>
 *             <option value="1981">1981</option>
 *             <option value="1982">1982</option>
 *             <option value="1983">1983</option>
 *             <option value="1984">1984</option>
 *             <option value="1985">1985</option>
 *     </select>
 * </div>
 * 
 * 
 * js code:
 * $(document).ready(function(){
 *      $("#myselect select").sSelect();
 * })
 * 
 * 2010.7.20  modifly by:WuGuangying
 * 1.add: param "_name"、"_id";
 * 2.add: $('.dropselectbox').show();
 * 3.add: selectWidth=$('#'+selectId+' select').parent().width();
 * 
 * 
 * 2010.7.22 modifly by :zhouguanqing
 * add if ie6  removeattr tabIndex;
 * 
 * 2010.8.16 modifly by :zhouguanqing
 * for ie6  remove $('#'+selectId).css({'z-index':selectZindex});   in  clearSelectMenu;
 * 
 * 
 */

(function($){
$.fn.extend({
    sSelect: function(switchId) {
        return this.each(function(i,obj){
        var _name="",_id="";
        if(this.name!=null)this.name.replace(/\./g,"_");
        if(this.id!=null)_id=this.id.replace(/\./g,"_");
        var selectId = (_name||_id)+'__jQSelect'+i||'__jQSelect'+i;
        if(obj.style.display != 'none' && $(this).parents()[0].id.indexOf('__jQSelect')<0){
            
        var tabindex = this.tabIndex||0;
        $(this).before("<div class='dropdown' id="+selectId+" tabIndex="+tabindex+"></div>").prependTo($("#"+selectId));
        var selectZindex = $(this).css('z-index'),selectIndex = $('#'+selectId+' option').index($('#'+selectId+' option:selected')[0]);
        $('#'+selectId).append('<div class="dropselectbox"><h4></h4><ul></ul></div>');
        $('#'+selectId+' h4').empty().append($('#'+selectId+' option:selected').text());
        var selectWidth=$('#'+selectId+' select').width();
        if(selectWidth==0 && $('#'+selectId+' select').parent().length){
            selectWidth=$('#'+selectId+' select').parent().width();
        }
        if($.browser.safari){selectWidth = selectWidth+15;}
        $('#'+selectId+' h4').css({width:selectWidth});
//        $('#'+selectId).find("ul").css({width:selectUlwidth+'px'});
        $('#'+selectId+' select').hide();
        $('.dropselectbox').show();
        //if ie6 remove tabIndex
        $.browser.msie?selectWidth=$('#'+selectId+' select').width():selectWidth = $('#'+selectId).width();
		if($.browser.msie)$('#'+selectId).removeAttr("tabIndex");
		
        $('#'+selectId+' div').hover(function(){
            $('#'+selectId+' h4').addClass("over");
        },function(){
            $('#'+selectId+' h4').removeClass("over");
        });
        $('#'+selectId)
        .bind("focus",function(){
            $.fn.clearSelectMenu(selectId,selectZindex);
            $('#'+selectId+' h4').addClass("over");
        })
        .bind("click",function(e){
            if($('#'+selectId+' ul').css("display") == 'block'){
                $.fn.clearSelectMenu(selectId,selectZindex);
                return false;
            }else{           
                $('#'+selectId+' h4').addClass("current");
                $('#'+selectId+' ul').show();
                var selectZindex = $(this).css('z-index');
                if ($.browser.msie || $.browser.opera){$('.dropdown').css({'position':'relative','z-index':'0'});}
                $('#'+selectId).css({'position':'relative','z-index':'999'});
                $.fn.setSelectValue(selectId);
                selectIndex = $('#'+selectId+' li').index($('.selectedli')[0]);
                var windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $(this).offset().top;
                var ulspace = $('#'+selectId+' ul').outerHeight(true);
                var windowspace2 = $(this).offset().top - $(window).scrollTop() - ulspace;
                windowspace < ulspace && windowspace2 > 0?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
                $(window).scroll(function(){
                    windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $('#'+selectId).offset().top;
                    windowspace < ulspace?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
                }); 
                
                if($('#'+selectId+' li').css('float')=='none'){
                  //  $('#'+selectId).find("ul").css({width:$('#'+selectId).width()+'px;'}); 
                    var selectUlwidth = selectWidth + parseInt($('#'+selectId+' h4').css("padding-left")) + parseInt($('#'+selectId+' h4').css("padding-right"))-2;
                    //alert(selectUlwidth);
                    $('#'+selectId+' ul').css({width:selectUlwidth+'px'});
                };
                $('#'+selectId+' li').click(function(e){
                        selectIndex = $('#'+selectId+' li').index(this);
                        var lastchild=$('#'+selectId+' li').length-1;
						//add by guanqing for web4s v3.0
						 //alert(selectIndex);
						if(selectIndex==lastchild && switchId=="selKeep"){
							 
							$("#nameOfFavorite").show();
						}
						else{
							$("#nameOfFavorite").hide();
							$("#blogFavPopWarn").hide();
						}

                        $.fn.keyDown(selectId,selectIndex);
                        $('#'+selectId+' h4').empty().append($('#'+selectId+' option:selected').text());
                        $.fn.clearSelectMenu(selectId,selectZindex);
                        e.stopPropagation();
                        e.cancelbubble = true;
                        
                        $('#'+selectId+' h4').attr("title",$('#'+selectId+' h4').text());
                })
                .hover(
                       function(){
                            $('#'+selectId+' li').removeClass("over");
                            $(this).addClass("over").addClass("selectedli");
                            selectIndex = $('#'+selectId+' li').index(this);
                            $('#'+selectId+' h4').attr("title",$('#'+selectId+' h4').text());
                        },
                        function(){
                            $(this).removeClass("over");
                        }
                );
            };
            e.stopPropagation();

            

           
               
            
        })
         .bind('mousewheel', function(e,delta) {
                e.preventDefault();
                var mousewheel = {
                    $obj : $('#'+selectId+' li.over'),
                    $slength : $('#'+selectId+' option').length,
                    mup:function(){
                        this.$obj.removeClass("over");
                        selectIndex == 0?selectIndex = 0:selectIndex--;
                        $.fn.keyDown(selectId,selectIndex);
                    },
                    mdown:function(){
                        this.$obj.removeClass("over");
                        selectIndex == (this.$slength - 1)?selectIndex = this.$slength - 1:selectIndex ++;
                        $.fn.keyDown(selectId,selectIndex);
                    }
                };
                delta>0?mousewheel.mup():mousewheel.mdown();
         })
        .bind("dblclick", function(){
            $.fn.clearSelectMenu(selectId,selectZindex);
            return false;
        })
        .bind("keydown",function(e){
            $(this).bind('keydown',function(e){
                if (e.keyCode == 40 || e.keyCode == 38 || e.keyCode == 35 || e.keyCode == 36){
                    return false;
                }
            });
            var $obj = $('#'+selectId+' li.over'),$slength = $('#'+selectId+' option').length;
            switch(e.keyCode){
                case 9:
                    return true;
                    break;
                case 13:
                    //enter
                    $.fn.clearSelectMenu(selectId,selectZindex);
                    break;
                case 27:
                    //esc
                    $.fn.clearSelectMenu(selectId,selectZindex);
                    break;
                case 33:
                    $obj.removeClass("over");
                    selectIndex = 0;
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                case 34:
                    $obj.removeClass("over");
                    selectIndex = ($slength - 1);
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                case 35:
                    $obj.removeClass("over");
                    selectIndex = ($slength - 1);
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                case 36:
                    $obj.removeClass("over");
                    selectIndex = 0;
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                case 38:
                    //up
                    e.preventDefault();
                    $obj.removeClass("over");
                    selectIndex == 0?selectIndex = 0:selectIndex--;
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                case 40:
                    //down
                    e.preventDefault();
                    $obj.removeClass("over");
                    selectIndex == ($slength - 1)?selectIndex = $slength - 1:selectIndex ++;
                    $.fn.keyDown(selectId,selectIndex);
                    break;
                default:
                    e.preventDefault();
                    break;
            };
        })
        .bind("blur",function(){
            $.fn.clearSelectMenu(selectId,selectZindex);
            $(window).unbind("scroll");
            return false;
        })
        .bind("selectstart",function(){
                return false;
        });
    }else if($(this).parents()[0].id.indexOf('__jQSelect')>0){        
        selectId = $(this).parents()[0].id;
        $.fn.setSelectValue(selectId);
        var selectWidth=$('#'+selectId+' select').width();
        if(selectWidth==0 && $('#'+selectId+' select').parent().length){
            selectWidth=$('#'+selectId+' select').parent().width();
        }
        if($.browser.safari){selectWidth = selectWidth+15;}
        $('#'+selectId+' h4').css({width:selectWidth});
  
        if(this.style.display != 'none'){$(this).hide();}
    }});},
    clearSelectMenu:function(selectId,selectZindex){
        if(selectId != undefined){
            selectZindex = selectZindex||'90';
            $('#'+selectId+' ul').empty().hide();
            $('#'+selectId+' h4').removeClass("over").removeClass("current");
            $('#'+selectId).css({'z-index':selectZindex});
        }
    },
    setSelectValue:function(sID){
        var content = [];
        $.each($('#'+sID+' option'), function(i){
            content.push("<li class='FixSelectBrowser'>"+$(this).text()+"</li>");
        });
        content = content.join('');
        $('#'+sID+' ul').html(content);
        $('#'+sID+' h4').html($('#'+sID+' option:selected').text());
        $('#'+sID+' li').eq($('#'+sID+' select')[0].selectedIndex).addClass("over").addClass("selectedli");
    },
    keyDown:function(sID,selectIndex){
        var $obj = $('#'+sID+' select');
        $obj[0].selectedIndex = selectIndex;
        $obj.change();
        $('#'+sID+' li:eq('+selectIndex+')').toggleClass("over");
        $('#'+sID+' h4').html($('#'+sID+' option:selected').text());
    }
});
var types = ['DOMMouseScroll', 'mousewheel'];
$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener )
            for ( var i=types.length; i; )
                this.addEventListener( types[--i], handler, false );
        else
            this.onmousewheel = handler;
    },  
    teardown: function() {
        if ( this.removeEventListener )
            for ( var i=types.length; i; )
                this.removeEventListener( types[--i], handler, false );
        else
            this.onmousewheel = null;
    }
};
$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});
function handler(event) {
    var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
    event = $.event.fix(event || window.event);
    event.type = "mousewheel";  
    if ( event.wheelDelta ) delta = event.wheelDelta/120;
    if ( event.detail     ) delta = -event.detail/3;
    args.unshift(event, delta);
    return $.event.handle.apply(this, args);
}
})(jQuery);